package appUtils;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.jhlibrarys.R;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

import static appUtils.SharedPreferencesManager.FIRST;

/**
 * Created by Joe_PC on 2015/8/5.
 */
public class FragmentDockUtil implements RadioGroup.OnCheckedChangeListener {
    private final List<Fragment> mFragmentList;
    private final FragmentActivity mFragmentActivity;
    private final int mFragmentContentId;
    private int mCurrentTab;

    public FragmentDockUtil(FragmentActivity fragmentActivity, List<Fragment> mFragmentList,
                            int whichRadioButton, int fragmentContentId, RadioGroup radioGroup) {
        this.mFragmentList = mFragmentList;
        this.mFragmentActivity = fragmentActivity;
        this.mFragmentContentId = fragmentContentId;
        this.mCurrentTab = whichRadioButton;
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
        SharedPreferencesManager msharedPreferencesManager = SharedPreferencesManager.
                getInstance(fragmentActivity.getApplicationContext());
        if (!msharedPreferencesManager.readBoolean(FIRST, true)) {
            addFragment(ft, mFragmentList.get(1));
            ft.hide(mFragmentList.get(1));
        }
        addFragment(ft, mFragmentList.get(mCurrentTab));
        ft.commit();
        RadioButton rb = (RadioButton) radioGroup.getChildAt(mCurrentTab);
        rb.setTextColor(ContextCompat.getColor(fragmentActivity, R.color.colorMainPurple));
        rb.setChecked(true);
        radioGroup.setOnCheckedChangeListener(this);
    }

    private void addFragment(FragmentTransaction ft, Fragment fragment) {
        if (fragment.isAdded()) {
            fragment.onResume();
        } else {
            ft.add(mFragmentContentId, fragment);
        }
    }

    @Override
    public void onCheckedChanged(final RadioGroup radioGroup, final int checkedId) {
        Observable.timer(20, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        int size = radioGroup.getChildCount();
                        for (int i = 0; i < size; i++) {
                            RadioButton tb = (RadioButton) radioGroup.getChildAt(i);
                            if (radioGroup.getChildAt(i).getId() == checkedId) {
                                Fragment fragment = mFragmentList.get(i);
                                FragmentTransaction ft = obtainFragmentTransaction(i);
                                tb.setTextColor(ContextCompat.getColor(mFragmentActivity, R.color.colorMainPurple));
                                getCurrentFragment().onPause();
                                addFragment(ft, fragment);
                                showTab(i);
                                ft.commitAllowingStateLoss();
                            } else {
                                tb.setTextColor(ContextCompat.
                                        getColor(mFragmentActivity, R.color.colorCommonTextGray));
                            }
                        }
                    }
                });
    }

    /**
     * switch fragment
     *
     * @param idx showTab index
     */
    private void showTab(int idx) {
        FragmentTransaction ft = obtainFragmentTransaction(idx);
        ft.show(mFragmentList.get(idx));
        ft.hide(mFragmentList.get(mCurrentTab));
        ft.commitAllowingStateLoss();
        mCurrentTab = idx;
    }

    private FragmentTransaction obtainFragmentTransaction(int index) {
        FragmentTransaction ft = mFragmentActivity.getSupportFragmentManager().beginTransaction();
        if (index > mCurrentTab) {
            ft.setCustomAnimations(R.anim.slide_fragmentbottommenu_left_in, R.anim.slide_fragmentbottommenu_left_out);
        } else {
            ft.setCustomAnimations(R.anim.slide_fragmentbottommenu_right_in, R.anim.slide_fragmentbottommenu_right_out);
        }
        return ft;
    }

    public int getCurrentTab() {
        return mCurrentTab;
    }

    public Fragment getCurrentFragment() {
        return mFragmentList.get(mCurrentTab);
    }


}
